/*
 * @Author       : ljkt
 * @Version      : 1.0
 * @Date         : 2021-10-05 18:00:43
 * @LastEditors: ljkt
 * @LastEditTime: 2025-01-27 19:47:16
 * @Description  :
 */

#include "./Common_Include.h"
Ram s_dev_buzxy_t gDev_BuzXy;

CONST s_1yiejie_t POWER_ON_TAB[] = {
    {880, 500, 20, 1},
    {880, 500, 240, 0},
    {1244, 500, 20, 1},
    {1244, 500, 240, 0},
    {1865, 500, 20, 1},
    {1865, 500, 240, 0},
    {2093, 500, 20, 1},
    {2093, 500, 240, 0},
    {2794, 500, 20, 1},
    {2794, 500, 240, 0},
    {2489, 500, 20, 1},
    {2489, 500, 220, 0},
    {2093, 500, 20, 1},
    {2093, 500, 200, 0},
    {3322, 500, 100, 1},
    {3322, 500, 400, 0},
    {3322, 500, 0, 0},
};
CONST s_1yiejie_t POWER_OFF_TAB[] = {
    {2637, 500, 40, 1},
    {2637, 500, 300, 0},
    {2217, 500, 20, 1},
    {2217, 500, 180, 0},
    {1975, 500, 20, 1},
    {1975, 500, 180, 0},
    {1661, 500, 20, 1},
    {1661, 500, 220, 0},
    {1319, 500, 100, 1},
    {1319, 500, 300, 0},
    {1319, 500, 0, 0},
};
CONST s_1yiejie_t TONG_YONG_TAB[] = {
    {1661, 500, 50, 1},
    {1661, 500, 300, 0},
    {1661, 500, 0, 0},
};
CONST s_1yiejie_t CUO_WU_TAB[] = {
    {1237, 500, 40, 1},
    {1237, 500, 100, 0},
    {1103, 500, 60, 1},
    {1103, 500, 300, 0},
    {1103, 500, 0, 0},
};
CONST s_1yiejie_t QI_DONG_TAB[] = {
    {1048, 500, 10, 1},
    {1048, 500, 140, 0},
    {1320, 500, 20, 1},
    {1320, 500, 80, 0},
    {1568, 500, 50, 1},
    {1568, 500, 300, 0},
    {1103, 500, 0, 0},
};
CONST s_1yiejie_t ZAN_TING_TAB[] = {
    {1568, 500, 10, 1},
    {1568, 500, 140, 0},
    {1320, 500, 20, 1},
    {1320, 500, 80, 0},
    {1048, 500, 50, 1},
    {1048, 500, 300, 0},
    {1048, 500, 0, 0},
};
CONST s_1yiejie_t JIE_SHU_TAB[] = {
    {1318, 500, 20, 1},
    {1318, 500, 300, 0},
    {988, 500, 20, 1},
    {988, 500, 100, 0},
    {416, 500, 20, 1},
    {416, 500, 120, 0},
    {660, 500, 50, 1},
    {660, 500, 250, 0},
    {988, 500, 150, 1},
    {988, 500, 150, 0},
    {1318, 500, 200, 1},
    {1318, 500, 300, 0},
    {1318, 500, 0, 0},
};
CONST s_1yiejie_t GONG_NENG_KAI_TAB[] = {
    {1048, 500, 10, 1},
    {1048, 500, 140, 0},
    {1320, 500, 20, 1},
    {1320, 500, 80, 0},
    {1568, 500, 50, 1},
    {1568, 500, 300, 0},
    {1568, 500, 0, 0},
};
CONST s_1yiejie_t GONG_NENG_GUAN_TAB[] = {
    {1660, 500, 10, 1},
    {1660, 500, 50, 0},
    {1244, 500, 50, 1},
    {1244, 500, 200, 0},
    {1244, 500, 0, 0},
};
CONST s_1yiejie_t BAO_CUO_TAB[] = {
    {784, 500, 10, 1},
    {784, 500, 140, 0},
    {1047, 500, 10, 1},
    {1047, 500, 140, 0},
    {1397, 500, 110, 1},
    {1397, 500, 140, 0},
    {1397, 500, 0, 0},
};
CONST s_1yiejie_t BAO_CUO_TAB[] = {
    {784, 500, 10, 1},
    {784, 500, 140, 0},
    {1047, 500, 10, 1},
    {1047, 500, 140, 0},
    {1397, 500, 110, 1},
    {1397, 500, 140, 0},
    {1397, 500, 0, 0},
};
CONST s_1yiejie_t ZI_DING_YI_TAB[] = {
    {588, 500, 30, 1},
    {588, 500, 200, 0},
    {588, 500, 0, 0},
};
CONST s_1yiejie_t TONG_ZI_JIE_TAB[] = {
    {440, 500, 30, 1},
    {440, 500, 200, 0},
    {440, 500, 0, 0},
};
CONST s_1yiejie_t YANG_MAO_TAB[] = {
    {494, 500, 30, 1},
    {494, 500, 200, 0},
    {494, 500, 0, 0},
};
CONST s_1yiejie_t KONG_QI_XI_TAB[] = {
    {392, 500, 30, 1},
    {392, 500, 200, 0},
    {392, 500, 0, 0},
};
CONST s_1yiejie_t YU_RONG_TAB[] = {
    {524, 500, 30, 1},
    {524, 500, 200, 0},
    {524, 500, 0, 0},
};
CONST s_1yiejie_t CHU_JUN_MAN_TAB[] = {
    {660, 500, 30, 1},
    {660, 500, 200, 0},
    {660, 500, 0, 0},
};
CONST s_1yiejie_t BIAO_ZHUN_TAB[] = {
    {698, 500, 30, 1},
    {698, 500, 200, 0},
    {698, 500, 0, 0},
};
CONST s_1yiejie_t JIA_FANG_TAB[] = {
    {784, 500, 30, 1},
    {784, 500, 200, 0},
    {784, 500, 0, 0},
};
CONST s_1yiejie_t TE_ZI_XI_TAB[] = {
    {880, 500, 30, 1},
    {880, 500, 200, 0},
    {880, 500, 0, 0},
};
CONST s_1yiejie_t KUAI_XI_TAB[] = {
    {988, 500, 30, 1},
    {988, 500, 200, 0},
    {988, 500, 0, 0},
};
CONST s_1yiejie_t DAN_TUO_XI_TAB[] = {
    {1046, 500, 30, 1},
    {1046, 500, 200, 0},
    {1046, 500, 0, 0},
};
CONST s_1yiejie_t PIAO_TUO_TAB[] = {
    {1174, 500, 30, 1},
    {1174, 500, 200, 0},
    {1174, 500, 0, 0},
};
#define XY_NAME_AND_TAB(name) {e_INDEX_XY_BUZ_##name, name}
typedef struct
{
    unsigned char index;
    s_1yiejie_t *yuepu_addr;
} s_xy_yuepu_t;

CONST s_xy_yuepu_t YuePuTab[] = {
    XY_NAME_AND_TAB(POWER_ON_TAB),
    XY_NAME_AND_TAB(POWER_OFF_TAB),
    XY_NAME_AND_TAB(TONG_YONG_TAB),
    XY_NAME_AND_TAB(CUO_WU_TAB),
    XY_NAME_AND_TAB(QI_DONG_TAB),
    XY_NAME_AND_TAB(ZAN_TING_TAB),
    XY_NAME_AND_TAB(JIE_SHU_TAB),
    XY_NAME_AND_TAB(GONG_NENG_KAI_TAB),
    XY_NAME_AND_TAB(GONG_NENG_GUAN_TAB),
    XY_NAME_AND_TAB(BAO_CUO_TAB),
    XY_NAME_AND_TAB(ZI_DING_YI_TAB),
    XY_NAME_AND_TAB(TONG_ZI_JIE_TAB),
    XY_NAME_AND_TAB(YANG_MAO_TAB),
    XY_NAME_AND_TAB(KONG_QI_XI_TAB),
    XY_NAME_AND_TAB(YU_RONG_TAB),
    XY_NAME_AND_TAB(CHU_JUN_MAN_TAB),
    XY_NAME_AND_TAB(BIAO_ZHUN_TAB),
    XY_NAME_AND_TAB(JIA_FANG_TAB),
    XY_NAME_AND_TAB(TE_ZI_XI_TAB),
    XY_NAME_AND_TAB(KUAI_XI_TAB),
    XY_NAME_AND_TAB(DAN_TUO_XI_TAB),
    XY_NAME_AND_TAB(PIAO_TUO_TAB),
};

void Dev_BuzXy_Init(void)
{
    memset(&gDev_BuzXy, 0, sizeof(gDev_BuzXy)); /* 数据初始化 */
}

void Drv_BuzXy_Scan(void)
{
    static unsigned char step = 0, drv_xy_buz_tick = 0;
    static unsigned short tick = 0;
    static s_1yiejie_t *_1yinjie_addr, *_1yinjie_start_addr; /* 乐谱地址 */
    unsigned char i;

    if (Drv_Autoload_Ntick_u8(&drv_xy_buz_tick, 10ul))
    {
        /* 声音立即更新 */
        if (gDev_BuzXy.UpFlag)
        {
            step = 0;
        }
        switch (step)
        {
        case 0:
            if (gDev_BuzXy.UpFlag == 0)
            {
                DRV_BUZXY_POWER(REF_FALSE);
                DRV_BUZXY_FREQ_DUTY(4000ul, 0);
                return;
            }
            gDev_BuzXy.UpFlag = 0;
            tick = Drv_Get_Tick_u16();
            if (gDev_BuzXy.Cmd < e_INDEX_XY_BUZ_MAX) /* 有效长度产生，说明有音乐数据 */
            {
                for (i = 0; i < ARR_CNT(YuePuTab); i++)
                {
                    if (YuePuTab[i].index == gDev_BuzXy.Cmd)
                    {
                        _1yinjie_start_addr = _1yinjie_addr = YuePuTab[i].yuepu_addr; /* 获取乐谱首地址 */
                        step = 1;
                        break;
                    }
                }
            }

            break;
        case 1:
            DRV_BUZXY_POWER(_1yinjie_addr->Power);
            DRV_BUZXY_FREQ_DUTY(_1yinjie_addr->F_Pwm, _1yinjie_addr->Duty_Pwm);
            if (Drv_Autoload_Ntick_u16(&tick, _1yinjie_addr->Time))
            {
                _1yinjie_addr++;
            }
            if (_1yinjie_addr->Time == 0)
            {
                step = 0;
            }
            else if (_1yinjie_addr->Time == 0xffff) // 如果处是0xffff，则表示该乐谱再次循环
            {
                _1yinjie_addr = _1yinjie_start_addr; /* 获取乐谱首地址 */
            }
            break;
        default:
            step = 0;
            break;
        }
    }
}
